/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ep.persistence.controllers;

import ep.persistence.controllers.exceptions.NonexistentEntityException;
import ep.persistence.entities.T008Dosage;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import ep.persistence.entities.T013Frequency;
import ep.persistence.entities.T009Medicine;
import ep.persistence.entities.T006Prescription;

/**
 *
 * @author Spyros
 */
public class T008DosageJpaController {

    public T008DosageJpaController() {
        emf = Persistence.createEntityManagerFactory("EprescPersistencePU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(T008Dosage t008Dosage) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T013Frequency t013Frequency = t008Dosage.getT013Frequency();
            if (t013Frequency != null) {
                t013Frequency = em.getReference(t013Frequency.getClass(), t013Frequency.getId());
                t008Dosage.setT013Frequency(t013Frequency);
            }
            T009Medicine t009Medicine = t008Dosage.getT009Medicine();
            if (t009Medicine != null) {
                t009Medicine = em.getReference(t009Medicine.getClass(), t009Medicine.getId());
                t008Dosage.setT009Medicine(t009Medicine);
            }
            T006Prescription t006Prescription = t008Dosage.getT006Prescription();
            if (t006Prescription != null) {
                t006Prescription = em.getReference(t006Prescription.getClass(), t006Prescription.getId());
                t008Dosage.setT006Prescription(t006Prescription);
            }
            em.persist(t008Dosage);
            if (t013Frequency != null) {
                t013Frequency.getT008DosageCollection().add(t008Dosage);
                t013Frequency = em.merge(t013Frequency);
            }
            if (t009Medicine != null) {
                t009Medicine.getT008DosageCollection().add(t008Dosage);
                t009Medicine = em.merge(t009Medicine);
            }
            if (t006Prescription != null) {
                t006Prescription.getT008DosageCollection().add(t008Dosage);
                t006Prescription = em.merge(t006Prescription);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(T008Dosage t008Dosage) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T008Dosage persistentT008Dosage = em.find(T008Dosage.class, t008Dosage.getId());
            T013Frequency t013FrequencyOld = persistentT008Dosage.getT013Frequency();
            T013Frequency t013FrequencyNew = t008Dosage.getT013Frequency();
            T009Medicine t009MedicineOld = persistentT008Dosage.getT009Medicine();
            T009Medicine t009MedicineNew = t008Dosage.getT009Medicine();
            T006Prescription t006PrescriptionOld = persistentT008Dosage.getT006Prescription();
            T006Prescription t006PrescriptionNew = t008Dosage.getT006Prescription();
            if (t013FrequencyNew != null) {
                t013FrequencyNew = em.getReference(t013FrequencyNew.getClass(), t013FrequencyNew.getId());
                t008Dosage.setT013Frequency(t013FrequencyNew);
            }
            if (t009MedicineNew != null) {
                t009MedicineNew = em.getReference(t009MedicineNew.getClass(), t009MedicineNew.getId());
                t008Dosage.setT009Medicine(t009MedicineNew);
            }
            if (t006PrescriptionNew != null) {
                t006PrescriptionNew = em.getReference(t006PrescriptionNew.getClass(), t006PrescriptionNew.getId());
                t008Dosage.setT006Prescription(t006PrescriptionNew);
            }
            t008Dosage = em.merge(t008Dosage);
            if (t013FrequencyOld != null && !t013FrequencyOld.equals(t013FrequencyNew)) {
                t013FrequencyOld.getT008DosageCollection().remove(t008Dosage);
                t013FrequencyOld = em.merge(t013FrequencyOld);
            }
            if (t013FrequencyNew != null && !t013FrequencyNew.equals(t013FrequencyOld)) {
                t013FrequencyNew.getT008DosageCollection().add(t008Dosage);
                t013FrequencyNew = em.merge(t013FrequencyNew);
            }
            if (t009MedicineOld != null && !t009MedicineOld.equals(t009MedicineNew)) {
                t009MedicineOld.getT008DosageCollection().remove(t008Dosage);
                t009MedicineOld = em.merge(t009MedicineOld);
            }
            if (t009MedicineNew != null && !t009MedicineNew.equals(t009MedicineOld)) {
                t009MedicineNew.getT008DosageCollection().add(t008Dosage);
                t009MedicineNew = em.merge(t009MedicineNew);
            }
            if (t006PrescriptionOld != null && !t006PrescriptionOld.equals(t006PrescriptionNew)) {
                t006PrescriptionOld.getT008DosageCollection().remove(t008Dosage);
                t006PrescriptionOld = em.merge(t006PrescriptionOld);
            }
            if (t006PrescriptionNew != null && !t006PrescriptionNew.equals(t006PrescriptionOld)) {
                t006PrescriptionNew.getT008DosageCollection().add(t008Dosage);
                t006PrescriptionNew = em.merge(t006PrescriptionNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = t008Dosage.getId();
                if (findT008Dosage(id) == null) {
                    throw new NonexistentEntityException("The t008Dosage with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T008Dosage t008Dosage;
            try {
                t008Dosage = em.getReference(T008Dosage.class, id);
                t008Dosage.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The t008Dosage with id " + id + " no longer exists.", enfe);
            }
            T013Frequency t013Frequency = t008Dosage.getT013Frequency();
            if (t013Frequency != null) {
                t013Frequency.getT008DosageCollection().remove(t008Dosage);
                t013Frequency = em.merge(t013Frequency);
            }
            T009Medicine t009Medicine = t008Dosage.getT009Medicine();
            if (t009Medicine != null) {
                t009Medicine.getT008DosageCollection().remove(t008Dosage);
                t009Medicine = em.merge(t009Medicine);
            }
            T006Prescription t006Prescription = t008Dosage.getT006Prescription();
            if (t006Prescription != null) {
                t006Prescription.getT008DosageCollection().remove(t008Dosage);
                t006Prescription = em.merge(t006Prescription);
            }
            em.remove(t008Dosage);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<T008Dosage> findT008DosageEntities() {
        return findT008DosageEntities(true, -1, -1);
    }

    public List<T008Dosage> findT008DosageEntities(int maxResults, int firstResult) {
        return findT008DosageEntities(false, maxResults, firstResult);
    }

    private List<T008Dosage> findT008DosageEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(T008Dosage.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public T008Dosage findT008Dosage(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(T008Dosage.class, id);
        } finally {
            em.close();
        }
    }

    public int getT008DosageCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<T008Dosage> rt = cq.from(T008Dosage.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
